package com.google.gwt.it2tool.client;

import java.util.Comparator;
import java.util.TreeMap;
import java.util.TreeSet;

import com.google.gwt.it2tool.client.ClientDataCache.DataChangeHandler;

public class FactorySorter {
	
	private TreeSet<Factory> data;
	private SortedBy sortedBy = SortedBy.UNDEFINED;
	private ClientDataCache dataCache;
	private Comparator<Factory> current;
	private FactoryDataChangeHandler fChangeHandler;
	
	public FactorySorter(FactoryDataChangeHandler fChangeHandler){
		this.fChangeHandler = fChangeHandler; 
		dataCache = ClientDataCache.getInstance();
		dataCache.addDataChangeHandler(changeHandler);
		data = new TreeSet<Factory>(dataCache.getFactories().values());
	}
	
	public SortedBy getSortedBy(){
		return sortedBy;
	}
	
	public TreeSet<Factory> getFactories(){
		return data;
	}
	
	public void sortByNameAsc(){
		current = nameAscComparator;
		sortedBy = SortedBy.NAME_ASC;
		sortByCurrentCompartor();		
	}
	
	public void sortByNameDesc(){
		current = nameDescComparator;
		sortedBy = SortedBy.NAME_DESC;
		sortByCurrentCompartor();		
	}
	
	public void sortByCustom(){
		current = customComparator;
		sortedBy = SortedBy.CUSTOM;
		sortByCurrentCompartor();	
	}
	
	private void sortByCurrentCompartor(){
		TreeSet<Factory> ret = new TreeSet<Factory>(current);
		ret.addAll(data);
		data = ret;
		fChangeHandler.onFactoryDataChanged();
	}

	private Comparator<Factory> nameAscComparator =  new Comparator<Factory>(){
		@Override
		public int compare(Factory o1, Factory o2) {
			return o1.getName().compareTo(o2.getName());
		}		
	};
	
	private Comparator<Factory> nameDescComparator =  new Comparator<Factory>(){
		@Override
		public int compare(Factory o1, Factory o2) {
			return o1.getName().compareTo(o2.getName())*-1;
		}		
	};
	
	private Comparator<Factory> customComparator =  new Comparator<Factory>(){
		@Override
		public int compare(Factory o1, Factory o2) {
			CustomFactoryOrder  order = dataCache.getCustomFactoryOrder();
			if (order!=null){
				int f1Pos = order.getPositionForFactory(o1);
				int f2Pos = order.getPositionForFactory(o2);
				if (f1Pos==0)return -1;
				if (f2Pos==0)return 1;
				return new Integer(f1Pos).compareTo(f2Pos);
			}else{
				return -1;
			}
		}		
	};
	
	private DataChangeHandler changeHandler = new DataChangeHandler() {
		
		@Override
		public void onRecipeDataChanged() {
			// TODO Auto-generated method stub			
		}
		
		@Override
		public void onPrototypeDataChanged() {
			// TODO Auto-generated method stub			
		}
		
		@Override
		public void onMassProductionDataChanged() {
			// TODO Auto-generated method stub			
		}
		
		@Override
		public void onFactoryDataChanged() {
			data = new TreeSet<Factory>(current);
			data.addAll(dataCache.getFactories().values());
			fChangeHandler.onFactoryDataChanged();
		}
	};

	
	public enum SortedBy{
		NAME_ASC,NAME_DESC,CUSTOM,UNDEFINED
	};
	
	public interface FactoryDataChangeHandler{
		public void onFactoryDataChanged();
	}
}
